<template>
  <div class="memoContainer">
    <h1>备忘录</h1>

    <input
      type="text"
      v-model="newItem"
      @keyup.enter="addItem"
      placeholder="请输入内容，按回车添加"
    />

    <ul v-show="items.length > 0">
      <li v-for="(item, index) in items" :key="index">
        <span>{{ item }}</span>
        <button @click="removeItem(index)">删除</button>
      </li>
    </ul>

    <div class="emptyTip" v-if="items.length == 0">暂无备忘事项，请添加...</div>

    <div class="footer">
      <span v-text="`共${items.length}条记录`"></span>
      <button @click="clearAll" v-show="items.length > 0">清空</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newItem: "",
      items: [],
    };
  },
  methods: {
    addItem() {
      if (this.newItem.trim() == "") return;
      this.items.push(this.newItem);
      this.newItem = "";
    },

    removeItem(index) {
      this.items.splice(index, 1);
    },

    clearAll() {
      this.items = [];
    },
  },
};
</script>

<style scoped>
.memoContainer {
  width: 500px;
  margin: 50px auto;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
}

input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  box-sizing: border-box;
  margin-bottom: 20px;
  transition: border-color 0.3s;
}

input:focus {
  outline: none;
  border-color: #4caf50;
}

ul {
  padding: 0;
  margin: 0 0 20px 0;
}

li {
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  background-color: white;
  border-radius: 4px;
  margin-bottom: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

li:hover {
  transform: translateX(5px);
}

li span {
  flex-grow: 1;
  color: #333;
}

button {
  padding: 6px 12px;
  background-color: #f44336;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #d32f2f;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 15px;
  border-top: 1px solid #ddd;
  color: #666;
}

.footer button {
  background-color: #9e9e9e;
}

.footer button:hover {
  background-color: #757575;
}

.emptyTip {
  text-align: center;
  color: #888;
  padding: 20px 0;
  font-style: italic;
}
</style>